﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using KPIS.GERP.GFMS.APP.Reports;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;


namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class ViewReport : System.Web.UI.Page
    {
        Main main = new Main();

        public void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LoadReport(string ReportName, string SeqFrom, string SeqTo, string Seq)
        {
            Session["ReportFromSeq"] = SeqFrom;
            Session["ReporttoSeq"] = SeqTo;
            Session["ReportSeq"] = Seq;
            switch (ReportName)
            {
                case "Receipt":
                    LoadReceipt(Seq);
                    break;
                case "ProjectOperation":
                    LoadProjectOperation(Seq);
                    break;
                case "MoneyReqContracts":
                    LoadMoneyReqContracts(Seq);
                    break;
                case "SumUpPayment":
                    LoadSumUpPayment(SeqFrom, SeqTo);
                    break;
                case "SumUpReceive":
                    LoadSumUpReceive(SeqFrom, SeqTo);
                    break;
                case "SumUpReceive2":
                    LoadSumUpReceive2(SeqFrom, SeqTo);
                    break;
            }
        }

        protected void LoadReceipt(string Seq)
        {
            Receipt Receipt1 = new Receipt();
            IBLL_FIM_Receives r = new B_FIM_Receives();
            Receipt1.SetDataSource(r.Report_Receipt(Seq));
            CrystalReportViewer1.ReportSource = Receipt1;
        }

        protected void LoadProjectOperation(string Seq)
        {
            ProjectOperation ProjectOperation1 = new ProjectOperation();
            IBLL_BGM_ProjectOperation op = new B_BGM_ProjectOperation();
            ProjectOperation1.SetDataSource(op.Report_ProjectOperation(Seq));
            ProjectOperation1.OpenSubreport("ProjectOperationSub.rpt");
            ProjectOperation1.Subreports[0].SetDataSource(op.SubReport_ProjectOperation(Seq));
            string strProductList = op.GetOptionProduct(Seq);
            ProjectOperation1.SetParameterValue(0, strProductList);
            CrystalReportViewer1.ReportSource = ProjectOperation1;
        }

        protected void LoadMoneyReqContracts(string Seq)
        {
            MoneyReqContractNew MoneyReqContracts1 = new MoneyReqContractNew();
            IBLL_FIM_MoneyRequests contract = new B_FIM_MoneyRequests();
            DataTable dtb = contract.Report_Contract(Seq);
            MoneyReqContracts1.SetDataSource(dtb);
            MoneyReqContracts1.OpenSubreport("MoneyReqContractsSub.rpt");
            MoneyReqContracts1.Subreports[0].SetDataSource(contract.Report_ContractSub(Seq));

            string strMoneyTextThai = main.MoneyTextThai(Convert.ToDouble(dtb.Rows[0]["amount"]));
            MoneyReqContracts1.SetParameterValue("param0", strMoneyTextThai);
            MoneyReqContracts1.SetParameterValue("param0", strMoneyTextThai, "MoneyReqContractsSub.rpt");
            CrystalReportViewer1.ReportSource = MoneyReqContracts1;
        }

        protected void LoadSumUpPayment(string SeqFrom, string SeqTo)
        {
                SumUpPayment SumUpPayment1 = new SumUpPayment();
                IBLL_FIM_Payments pay = new B_FIM_Payments();
                
                SumUpPayment1.SetDataSource(pay.Report_SumUpPaymentFT(SeqFrom, SeqTo, null));
                SumUpPayment1.SetParameterValue(0, SeqFrom);
                SumUpPayment1.SetParameterValue(1, SeqTo);
                CrystalReportViewer1.ReportSource = SumUpPayment1;
        }

        protected void LoadSumUpReceive(string SeqFrom, string SeqTo)
        {
            SumUpReceive SumUpReceive1 = new SumUpReceive();
            IBLL_FIM_Receives rec = new B_FIM_Receives();
            SumUpReceive1.SetDataSource(rec.Report_SumUpReceiveFT(SeqFrom, SeqTo, ""));
            SumUpReceive1.SetParameterValue(0, SeqFrom);
            SumUpReceive1.SetParameterValue(1, SeqTo);
            CrystalReportViewer1.ReportSource = SumUpReceive1;
        }

        protected void LoadSumUpReceive2(string SeqFrom, string SeqTo)
        {
            SumUpReceive2 SumUpReceive2 = new SumUpReceive2();
            IBLL_FIM_Receives rec = new B_FIM_Receives();
            SumUpReceive2.SetDataSource(rec.Report_SumUpReceiveFT(SeqFrom, SeqTo, ""));
            SumUpReceive2.SetParameterValue(0, SeqFrom);
            SumUpReceive2.SetParameterValue(1, SeqTo);
            CrystalReportViewer1.ReportSource = SumUpReceive2;
        }

        protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {
            string ReportName = Request.QueryString["name"];
            string SeqFrom = Session["ReportFromSeq"].ToString();
            string SeqTo = Session["ReporttoSeq"].ToString();
            if (Session["ReportSeq"] == null)
            {
                LoadReport(ReportName, SeqFrom, SeqTo, null);
            }
            else if (Session["ReportSeq"] != null)
            {
                string Seq = Session["ReportSeq"].ToString();
                LoadReport(ReportName, null, null, Seq);
            }
            else
            {
                string Seq = Session["ReportSeq"].ToString();
                LoadReport(ReportName, SeqFrom, SeqTo, Seq);
            }
        }
    }
}
